package cc.mrbird.febs.system.controller;


import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.domain.FebsResponse;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.system.domain.Area;
import cc.mrbird.febs.system.service.AreaService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author dtwei
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/tArea")
public class AreaController {

    private String message;

    @Resource
    private AreaService areaService;

    @GetMapping
    @RequiresPermissions("area:view")
    public Map<String, Object> areaList(Area tArea) {
        return this.areaService.findAreas(tArea);
    }

    @Log("新增区域")
    @PostMapping
    @RequiresPermissions("area:add")
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse addArea(@RequestBody @Valid Area area) throws FebsException {
        try {
            this.areaService.createArea(area);
            return new FebsResponse().code("200").message("新增区域成功").status("success");
        } catch (FebsException e) {
            throw e;
        } catch (Exception e) {
            message = "新增区域失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除区域")
    @DeleteMapping("/{areaIds}")
    @RequiresPermissions("area:delete")
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse deleteAreas(@NotBlank(message = "{required}") @PathVariable String areaIds) throws FebsException {
        try {
            String[] ids = areaIds.split(StringPool.COMMA);
            this.areaService.deleteAreas(ids);
            return new FebsResponse().code("200").message("删除区域成功").status("success");
        } catch (FebsException e) {
            throw e;
        } catch (Exception e) {
            message = "删除区域失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改区域")
    @PutMapping
    @RequiresPermissions("area:update")
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse updateArea(@RequestBody @Valid Area area) throws FebsException {
        try {
            this.areaService.updateArea(area);
            return new FebsResponse().code("200").message("修改区域成功").status("success");
        } catch (FebsException e) {
            throw e;
        } catch (Exception e) {
            message = "修改区域失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @PostMapping("excel")
    @RequiresPermissions("area:export")
    public void export(@RequestBody Area area, HttpServletResponse response) throws FebsException {
        try {
            List<Area> menus = this.areaService.findAreaList(area);
            ExcelKit.$Export(Area.class, response).downXlsx(menus, false);
        } catch (Exception e) {
            message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
